package urjc.as.emergencies.client.jpanels.missions;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextPane;
import javax.swing.JTree;
import javax.swing.border.TitledBorder;
import javax.swing.tree.DefaultMutableTreeNode;

import net.miginfocom.swing.MigLayout;

import org.restlet.data.Form;
import org.restlet.data.Status;

import urjc.as.emergencies.client.CommToolkit;
import urjc.as.emergencies.client.Node;
import urjc.as.emergencies.client.TextArea;
import urjc.as.emergencies.data.PatientRecord;

@SuppressWarnings("serial")
public class PanelHistorial extends JPanel {
	
	private TextArea text;
	private JTree tree;
	private JTextPane textPaneAlergias;
	private JTextPane textPaneMedicinas;
	private JTextPane textPaneEnfermedades;
	

	/**
	 * Create the panel.
	 */
	public PanelHistorial(TextArea textArea, JTree jtree, PatientRecord historial) {
		this.text = textArea;
		this.tree = jtree;
		
		setBorder(new TitledBorder(null, "Historial del Paciente", TitledBorder.LEADING, TitledBorder.TOP, null, Color.BLUE));
		setLayout(new MigLayout("", "[][grow]", "[][grow][][grow][][grow][][]"));
		
		JSeparator separator_3 = new JSeparator();
		add(separator_3, "cell 0 0 2 1,grow");
		
		JLabel lblAlergas = new JLabel("Alergías");
		lblAlergas.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblAlergas, "cell 0 1");
		
		textPaneAlergias = new JTextPane();
		add(textPaneAlergias, "cell 1 1,grow");
		
		JSeparator separator = new JSeparator();
		add(separator, "cell 0 2 2 1,grow");
		
		JLabel lblMedicinas = new JLabel("Medicinas");
		lblMedicinas.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblMedicinas, "cell 0 3");
		
		textPaneMedicinas = new JTextPane();
		add(textPaneMedicinas, "cell 1 3,grow");
		
		JSeparator separator_1 = new JSeparator();
		add(separator_1, "cell 0 4 2 1,grow");
		
		JLabel lblEnfermedades = new JLabel("Enfermedades");
		lblEnfermedades.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblEnfermedades, "cell 0 5");
		
		textPaneEnfermedades = new JTextPane();
		add(textPaneEnfermedades, "cell 1 5,grow");
		
		JSeparator separator_2 = new JSeparator();
		add(separator_2, "cell 0 6 2 1,grow");
		
		JButton btnCommit = new JButton("Actualizar campos");
		btnCommit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
				
				if (node != null) {
					// Obtenemos la URI asociada al nodo seleccionado
					Node nodeInfo = (Node) node.getUserObject();
					String targetURI = nodeInfo.getUri();
										
					//Creo el formulario con las modificaciones
					Form form = new Form();
					form.add(PatientRecord.ALLERGIES, textPaneAlergias.getText());
					form.add(PatientRecord.DISEASES, textPaneEnfermedades.getText());
					form.add(PatientRecord.MEDICINES, textPaneMedicinas.getText());
					
					// Hacemos la petición para crear un nuevo mailbox de
					// usuario, sobre la URL adecuada
					Status state = CommToolkit.sendPatch(targetURI, form);
					
					// Se muestra el estado
					text.append(state.getDescription());
					
					// Deseleccionamos el nodo del árbol
					tree.clearSelection();
				}
				
			}
		});
		add(btnCommit, "cell 1 7,alignx center,aligny baseline");
		
		//Actualizar el panel historial con la informacion almacenada
		textPaneAlergias.setText(historial.getAlergias());
		textPaneEnfermedades.setText(historial.getEnfermedades());
		textPaneMedicinas.setText(historial.getMedicamentos());

	}

}
